﻿using LdtJudge.DataEngine.CallInterface;
using LdtJudge.DataEngine.CallInterface.CommHelp;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.DataModel.SystemtableModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace LdtJudge.DataEngine.Data
{
    public class DALSYS_Express_Source
    {
        
        private static IFreeSql freeSql= FreeSQLHelp.GetFreeSQL("Ldt_JudgeDocSystem");
        public DALSYS_Express_Source()
        {
           
        }

        /// <summary>
        /// 获取实体对象
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public virtual List<MSYS_Express_Source> GetListArrayWhere(string strWhere)
        {
            //strSql.Append("select t.* from SYS_Express_Source t inner join SYS_Argument b on t.ES_Purpose=b.AM_ID  ");
            string b = freeSql.Select<SYS_Express_Source, Model.DataModel.SystemtableModel.SYS_Argument> ().InnerJoin((a,b)=> a.ES_Purpose==b.AM_ID).Where(strWhere).ToSql();
            string c= freeSql.Select<SYS_Express_Source, Model.DataModel.SystemtableModel.SYS_Argument>().InnerJoin((a, b) => a.ES_Purpose == b.AM_ID).ToSql();
            //List<SYS_Express_Source> table= freeSql.Select<SYS_Express_Source, Model.DataModel.SystemtableModel.SYS_Argument>().InnerJoin((a, b) => a.ES_Purpose == b.AM_ID).Where(strWhere).ToList();
            List<MSYS_Express_Source> table= freeSql.Select<SYS_Express_Source, Model.DataModel.SystemtableModel.SYS_Argument>().InnerJoin((a, b) => a.ES_Purpose == b.AM_ID).Where(strWhere).ToList<MSYS_Express_Source>();
            if (table.Count>0)
            {

                return table;
                
            }
            string sql = freeSql.Select<SYS_Express_Source, Model.DataModel.SystemtableModel.SYS_Argument>().InnerJoin((a, b) => a.ES_Purpose == b.AM_ID).Where(strWhere).ToSql();
            return new List<MSYS_Express_Source>(); 
        }

     

        //public virtual List<MSYS_Express_Source> GetListArrayWhere(string strWhere)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select t.* from SYS_Express_Source t inner join SYS_Argument b on t.ES_Purpose=b.AM_ID  ");
        //    if (strWhere.Trim() != "")
        //    {
        //        strSql.Append(" where  " + strWhere);
        //    }

        //    IDbCommand dbCommand = sqhandle.GetSqlHelper.GetCommand(strSql.ToString());
        //    var query = from DataRow item in sqhandle.GetSqlHelper.ExecuteReader(dbCommand).Rows select ReaderBind(item);
        //    return query.ToList<MSYS_Express_Source>();
        //}
        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        //public virtual List<MSYS_Express_Source> GetListArray(string strWhere)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select t.* from SYS_Express_Source t inner join SYS_Argument b on t.ES_Purpose=b.AM_ID  ");
        //    if (strWhere.Trim() != "")
        //    {
        //        strSql.Append(" where  " + strWhere);
        //    }

        //    IDbCommand dbCommand = sqhandle.GetSqlHelper.GetCommand(strSql.ToString());
        //    var query = from DataRow item in sqhandle.GetSqlHelper.ExecuteReader(dbCommand).Rows select ReaderBind(item);
        //    return query.ToList<MSYS_Express_Source>();
        //}

        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="cc_id">ccid</param>
        /// <param name="sourceName">数据源ID</param>
        /// <returns></returns>
        public virtual List<MSYS_Express_Source> GetListArray(string cc_id,string sourceName,string paragraphCode)
        {
            //var table = freeSql.Select<SYS_Express_Source>().Where(a=> a.CC_ID==cc_id && a.Source_ID.IndexOf(sourceName)>-1).ToList();
            var table = freeSql.Select<SYS_Express_Source>().Where(a=> a.CC_ID==cc_id &&a.Source_ParagraphID.ToLower()==paragraphCode.ToLower() && a.Source_ID.IndexOf(sourceName)>-1).ToList<MSYS_Express_Source>();
            if (table.Count > 0)
            {
                return table;
                //var query = from SYS_Express_Source item in table.ToList() select SettingCenterClient.CopySameFieldsObject<MSYS_Express_Source>(item);
                //return query.ToList<MSYS_Express_Source>();
            }
            return new List<MSYS_Express_Source>();
        }
        /// <summary>
        /// 对象实体绑定数据
        /// 20170509 ljx 修改;
        /// </summary>
        public MSYS_Express_Source ReaderBind(DataRow dataReader)
        {
            MSYS_Express_Source model = new MSYS_Express_Source();
            model.ES_ID = dataReader["ES_ID"] == null ? string.Empty : dataReader["ES_ID"].ToString();
            model.CC_ID = dataReader["CC_ID"] == null ? string.Empty : dataReader["CC_ID"].ToString();
            model.Source_ID = dataReader["Source_ID"] == null ? string.Empty : dataReader["Source_ID"].ToString();
            model.Source_ParagraphID = dataReader["Source_ParagraphID"] == null ? string.Empty : dataReader["Source_ParagraphID"].ToString();
            model.Source_Name = dataReader["Source_Name"] == null ? string.Empty : dataReader["Source_Name"].ToString();
            model.Source_ParagraphName = dataReader["Source_ParagraphName"] == null ? string.Empty : dataReader["Source_ParagraphName"].ToString();
            model.CC_RegexInfoCode = dataReader["CC_RegexInfoCode"] == null ? string.Empty : dataReader["CC_RegexInfoCode"].ToString();

            if (dataReader["IsParagraphExtract"] != null && dataReader["IsParagraphExtract"] != DBNull.Value)
            {
                model.IsParagraphExtract = (int)dataReader["IsParagraphExtract"];
            }
            if (dataReader["IsEvidenceMatch"] != null && dataReader["IsEvidenceMatch"] != DBNull.Value)
            {
                model.IsEvidenceMatch = (int)dataReader["IsEvidenceMatch"];
            }
            return model;
        }
    }
}
